package com.youmu.sichuansfc.controller;

import com.youmu.sichuansfc.common.CommonRes;
import com.youmu.sichuansfc.domain.YoumuCompany;
import com.youmu.sichuansfc.domain.YoumuCompanyRisk;
import com.youmu.sichuansfc.dto.req.CompanyDto;
import com.youmu.sichuansfc.dto.rsp.CompanyDetail;
import com.youmu.sichuansfc.dto.rsp.CompanyT;
import com.youmu.sichuansfc.service.CompanyService;
import com.youmu.sichuansfc.util.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;

@Controller
public class CompanyController {

    @Autowired
    CompanyService companyService;

    /**
     * 企业信息录入
     * @param companyDto
     * @return
     * @throws Exception
     */
    @RequestMapping( value = "/company", method = RequestMethod.POST)
    public @ResponseBody CommonRes insert2(@RequestBody  CompanyDto companyDto)throws  Exception{
        System.out.println(new JSONObject(companyDto));
        try {
            YoumuCompany youmuCompany = companyService.insert(companyDto);
            return  CommonRes.success();
        }catch (Exception e){
            e.getMessage();
        }
        return CommonRes.fail();
    }


    /**
     *  查询企业
     * @param startTime 提交时间 开始
     * @param endTime   提交时间 截止
     * @param companyName 公司名
     * @param num 页码
     * @param size 每页size
     * @return
     * @throws Exception
     */
    @RequestMapping( value = "/company/search", method = RequestMethod.GET)
    public @ResponseBody CommonRes CompanySearch(
            @RequestParam(name = "startTime", required = false , defaultValue = "1999-12-12 12:12:12") String startTime,
            @RequestParam(name = "endTime", required = false, defaultValue = "2900-12-12 12:12:12") String endTime,
            @RequestParam(name = "companyName", required = false) String companyName,
            @RequestParam(name = "num", required = false, defaultValue = "0") int  num,
            @RequestParam(name = "size", required = false, defaultValue = "10") int size,
            @RequestParam(name = "companyStatus", required = false) Integer companyStatus )throws  Exception{

        System.out.println(" companyName = [" + companyName+"]");

        Timestamp stime = null;
        Timestamp etime = null;
        // check time
        if(StringUtils.isNotEmpty(startTime) || StringUtils.isNotEmpty(endTime)){
            try {
                stime = StringUtils.isNotEmpty(startTime)   ? DateUtil.parseStr(startTime): null;
                etime = StringUtils.isNotEmpty(endTime)   ? DateUtil.parseStr(endTime) : null;
                long eLongtime2 = etime.getTime() + 24L*60*60*1000-1;
                etime = new Timestamp(eLongtime2);//当天的最后一个凌晨
            }catch (Exception e){
                // return  CommonRes.checkFail("时间格式错误");
            }
        }
        // service
        try {
            CompanyT companyT = companyService.searchCompany(stime, etime, companyName, companyStatus,  num, size);
            return CommonRes.success(companyT);
        }catch (Exception e){
            return CommonRes.fail();

        }
    }

    /**
     * 查询某个企业详情
     * @param id 公司ID
     * @return
     */
    @RequestMapping(value = "/company/detail", method = RequestMethod.GET)
    public @ResponseBody CommonRes CompanyDetail(
            @RequestParam(name = "id") Long id){
        CompanyDetail companyDetail = companyService.companyDetail(id);
        if(companyDetail != null){
            return  CommonRes.success(companyDetail);
        }
        return CommonRes.checkFail("无效参数");
    }

    /**
     * 查询某个风险详情
     * @param id 风险ID
     * @return
     */
    @RequestMapping(value = "/company/riskdetail", method = RequestMethod.GET)
    public @ResponseBody CommonRes CompanyRiskDetail(
            @RequestParam(name = "id") Long id){
        YoumuCompanyRisk youmuCompanyRisk = companyService.companyRiskDetail(id);
        if(youmuCompanyRisk != null){
            return  CommonRes.success(youmuCompanyRisk);
        }
        return CommonRes.checkFail("找不到");
    }

    /**
     *  月异常数量
     * @return
     */
    @RequestMapping(value = "/index/Month", method =  RequestMethod.GET)
    public @ResponseBody CommonRes IndexMonth(){

//        String str = "[153, 123, 256, 231, 151, 288, 200, 256, 170, 168, 160, 280]";
//        List counts = new ArrayList();
//        for (int i =0 ; i < 12; i++){
//            Random random = new Random();
//
//            counts.add(random.nextInt(100));
//
//
//            //counts.add(100);
//        }
        int[] counts = {223, 245, 123, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        counts[2]= companyService.errCompany();
        Map map = new  HashMap();
        map.put("month", counts);
      return CommonRes.success(map);
    }


    /**
     *  灰黑 公司数量
     * @return
     */
    @RequestMapping(value = "/company/type/count", method =  RequestMethod.GET)
    public @ResponseBody CommonRes CompanyTypeCount(){

        return CommonRes.success(companyService.companyTypeCount());
    }
}
